package svc

import (
	"context"
	"gozero-microservices/user-service/rpc/internal/config"
	"gozero-microservices/user-service/rpc/internal/model"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	Config                   config.Config
	UserModel                model.MallUserModel
	UserDeliveryAddressModel model.MallUserDeliveryAddressModel
	Logger                   logx.Logger
}

func NewServiceContext(c config.Config) *ServiceContext {
	// ------------- 获取mysql连接开始 -------------
	datasource := ""
	switch c.Env {
	case "dev":
		datasource = c.MysqlConf.Dev.DataSource
	case "test":
		datasource = c.MysqlConf.Test.DataSource
	case "prod":
		datasource = c.MysqlConf.Prod.DataSource
	}
	sc := sqlx.NewMysql(datasource)
	// ------------- 获取mysql连接结束 -------------

	return &ServiceContext{
		Config:                   c,
		UserModel:                model.NewMallUserModel(sc),
		UserDeliveryAddressModel: model.NewMallUserDeliveryAddressModel(sc),
		Logger:                   logx.WithContext(context.Background()),
	}
}
